package ug.co.icom.kyazze.healthticket.services;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import ug.co.icom.kyazze.healthticket.DataBase.Database;
import ug.co.icom.kyazze.healthticket.data.Allergy;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

public class UpdaterService extends Service {

	String doc_value;
	Database query_handler;
	static final String TAG = "UpdaterService";
	static final int DELAY = 30; //seconds
	boolean running=false;

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		Log.d(TAG, "onCreated");
		super.onCreate();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d(TAG, "onStartCommand");
		running =true;

		SharedPreferences settings = getSharedPreferences("ULS", 0);
		final String user_phone = settings.getString("uName", null);

		new Thread() {
			public void run() {
				try {
					
					while(running){
						BufferedReader br = null;

						try {

							String sCurrentLine;

							br = new BufferedReader(new FileReader(Environment
									.getExternalStorageDirectory().getPath()
									+ "/"
									+ user_phone + ".txt"));

							while ((sCurrentLine = br.readLine()) != null) {
								doc_value = sCurrentLine;
								System.out.println(sCurrentLine);
							}

						} catch (IOException e) {
							e.printStackTrace();
						} finally {
							try {
								if (br != null)
									br.close();
							} catch (IOException ex) {
								ex.printStackTrace();
							}
						}

						SQLiteDatabase db = openOrCreateDatabase("mrDB",
								MODE_PRIVATE, null);
						query_handler = new Database(db);

						ArrayList<Allergy> all_object = query_handler
								.getUnSyc_AllAllergies();

						String a_name = all_object.get(0).getAllergy_name();
						String a_date = all_object.get(0)
								.getAllergy_date_of_diagnosis().toString();
						String a_severity = all_object.get(0).getAllergy_severity();
						String a_reaction = all_object.get(0).getAllergy_reaction();
						String a_occurence = all_object.get(0)
								.getAllergy_occurence();
						String a_comments = all_object.get(0)
								.getAllergy_additional_comments();

						// try catch block
						PutAllergyData doc = new PutAllergyData();
						// String val = "{\"$oid\":\"52f1e542eddcb50002001236\"}";
						doc.execute(doc_value, a_name, a_date, a_severity,
								a_reaction, a_occurence, a_comments);
						
						Thread.sleep(DELAY*1000);
					}
					
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();

		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		Log.d(TAG, "onDestroy");
		running =false;
		super.onDestroy();
	}

}
